package com.eric.husky.service;

import com.eric.husky.common.utils.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.eric.husky.dao.MessageDao;
import com.eric.husky.bean.Message;
import java.util.List;

@Service
public class MessageService {
    @Autowired
	private MessageDao messageDao;

	public Page<Message> list(Message message, Pageable page) {
		Example<Message> example = Example.of(message);
		return messageDao.findAll(example, page);
	}

	public Message get(Integer messageid) {
		return messageDao.findById(messageid).get();
	}

    public List<Message> findList() {
		return messageDao.findAll();
	}

	@Transactional
	public Message save(Message message) {
	    if(message.getMessageid()!=null){
	        Message messageOld = messageDao.findById(message.getMessageid()).orElse(null);
	        if(messageOld!=null){
	            UpdateUtil.copyNullProperties(message,messageOld);
                return messageDao.save(messageOld);
	        }
	    }
	    return messageDao.save(message);
	}

	@Transactional
	public void delete(Integer messageid) {
		messageDao.deleteById(messageid);
	}

}